<?php $this->headTitle(__('ACL Permission')); ?>
<div id="maincontent">
	<form action="" method="post" name="frmPermission" id="frmPermission" >
		<div>
		<label><?php echo __('Module') ?>:</label>
			<select name="module" id="module">
				<option value=""><?php echo __('Select Module'); ?></option>
				<?php if($this->modules): ?>
					<?php foreach($this->modules as $module): ?>
						<option value="<?php echo $module; ?>"><?php echo ucfirst($module); ?></option>
					<?php endforeach; ?>
				<?php endif; ?>
			</select>
			
			<label><?php echo __('Resource') ?>:</label>
			<select name="resource" id="resource">
			
			</select>
		</div>
		<br>
		
		<table>
			<thead>
				<th><?php echo __('Available Permissions'); ?></th>
				<th>&nbsp;</th>
				<th><?php echo __('Assigned Permissions'); ?></th>
			</thead>
			<tbody>
				<tr>
					<td>
						<select multiple="multiple" name="available[]" id="available" class="multi"></select>
					</td>
					<td align="center">
						<div><button type="button" id="add" disabled="disabled"> >> </button></div>
						<div><button type="button" id="remove" disabled="disabled"> << </button></div>
					</td>
					<td>
						<select multiple="multiple" name="assigned[]" id="assigned" class="multi"></select>
					</td>
				</tr>
			</tbody>
		</table>
		<?php echo $this->pagination();?>
	</form>
</div>
<script type="text/javascript">
$(document).ready(function(){

	//select module
	$('#module').change(function(){
		
		if($(this).val() != '')
		{
			var pdata = 'm='+$(this).val();
			showAjaxLoading(true);
			$.get('/admin/aclpermission/index/f/rs',pdata,function(data){
				$("#resource").children().remove();
				$('#available').children().remove();
				$('#assigned').children().remove();
				populateResources(data);
				showAjaxLoading(false);
			},'json');
		}
		else
		{
			$('#available').children().remove();
			$('#assigned').children().remove();
			$('#resource').children().remove();
		}
	});

	//select resource
	$('#resource').change(function(){

		if($(this).val() != '')
		{
			var pdata = "m="+$('#module').val()+"&r="+$(this).val();
			showAjaxLoading(true)
			$.get('/admin/aclpermission/index/f/per',pdata,function(data){
				$('#available').children().remove();
				$('#assigned').children().remove();
				populatePermissions(data)
				showAjaxLoading(false);
			},'json');
		}
	});

	//select permission
	$("#available").change(function () {
		$('#add').attr('disabled',false);
		$('#remove').attr('disabled',true);
		$("#assigned option:selected").attr('selected',false);
	});
	//select permission for revoke
	$("#assigned").change(function () {
		$('#add').attr('disabled',true);
		$('#remove').attr('disabled',false);
		$("#available option:selected").attr('selected',false);
	});

	//add permission
	$("#add").click(function () {
		if($("#available option:selected").length > 0)
		{
			var pdata = $('#frmPermission').serialize();
			$('#add').attr('disabled',true);
			showAjaxLoading(true)
			$.post('/admin/aclpermission/add',pdata,function(data){
				
				if(data)
				{
					$('#available').children().remove();
					$('#assigned').children().remove();
					populatePermissions(data)
				}
				showAjaxLoading(false);
			},'json');
		}
	});

	//remove permission
	$("#remove").click(function () {
		if($("#assigned option:selected").length > 0)
		{
			var pdata = $('#frmPermission').serialize();
			$('#remove').attr('disabled',true);
			showAjaxLoading(true)
			$.post('/admin/aclpermission/delete',pdata,function(data){

				if(data)
				{
					$('#available').children().remove();
					$('#assigned').children().remove();
					populatePermissions(data)
				}
				showAjaxLoading(false);
			},'json');
		}
	});

});

function populateResources(data)
{
	var dropdownList = $("#resource")[0];
			
	$.each(data.resources,function() {
			
			var option = new Option(this.description, this.id);
			// Use Jquery to get select list element
			if ($.browser.msie) {
				dropdownList.add(option);
			}
			else {
				dropdownList.add(option, null);
			}
		});
		
	populatePermissions(data)
}

function populatePermissions(data)
{
	var availableList = $("#available")[0];
	var assignedList = $("#assigned")[0];
	
	$.each(data.permissions.available,function(i,value) {

			var option = new Option(value, value);
			// Use Jquery to get select list element
			if ($.browser.msie) {
				availableList.add(option);
			}
			else {
				availableList.add(option, null);
			}
		});

	$.each(data.permissions.assigned,function(i,value) {

			var option = new Option(this.permission, this.id);
			// Use Jquery to get select list element
			if ($.browser.msie) {
				assignedList.add(option);
			}
			else {
				assignedList.add(option, null);
			}
		});
}

</script>